package com.me.components.fix {
	import com.me.components.map.GridLayer;
	import com.me.components.map.MapScene;
	import com.me.components.map.ResourceLabel;
	import com.me.obj.Building;

	import flash.events.MouseEvent;

	import mx.core.IVisualElement;

	import spark.components.Panel;
	import spark.components.Scroller;
	import spark.components.VGroup;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-19 上午10:34:52
	 */
	public class MapResourceLayer extends Panel {
		private static var _instance:MapResourceLayer;

		private var _scroller:Scroller;

		private var _container:VGroup

		public static function getInstance():MapResourceLayer {
			if (_instance == null) {
				_instance = new MapResourceLayer(new SingletonEnforce());
			}
			return _instance;
		}

		public function MapResourceLayer(enforce:SingletonEnforce) {
			if (enforce == null) {
				throw new Error("MapResourceLayer can not be intance");
			}
			width = 220;
		}

		override protected function commitProperties():void {
			super.commitProperties();
		}

		override protected function createChildren():void {
			super.createChildren();

			_scroller = new Scroller();
			addElement(_scroller);

			_container = new VGroup();
			_container.paddingTop = 5;
			_scroller.viewport = _container;
		}

		override protected function childrenCreated():void {
			super.childrenCreated();
			title = "地图资源库";
//			titleDisplay.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
		}

		private function startDragHandler(e:MouseEvent):void {
			startDrag(false);
			stage.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
		}

		/**
		 *
		 * @param e
		 *
		 */
		private function stopDragHandler(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			stopDrag();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			_scroller.width = width;
			_scroller.height = height - titleDisplay.measuredHeight; // - FlexGlobals.topLevelApplication.statusBar.height;
		}

		public function addResource(data:Building):void {
			var sprite:ResourceLabel = new ResourceLabel();
			sprite.text = data.desc;
			sprite.attachment = data;
			sprite.addEventListener(MouseEvent.CLICK, clickhandler);
			_container.addElement(sprite);
		}

		private function clickhandler(e:MouseEvent):void {
			MapScene.getInstance().selectBuilding(e.currentTarget.attachment.img);
		}

		public function clear():void {
			while (_container.numElements > 0) {
				_container.removeElementAt(0).removeEventListener(MouseEvent.CLICK, clickhandler);
			}
		}

		public function spritePropertyChanged(res:String):void {
			for (var i:int = 0; i < _container.numElements; i++) {
				var label:ResourceLabel = _container.getElementAt(i) as ResourceLabel;
				if (label.attachment.img == res) {
					label.text = label.attachment.desc;
					break;
				}
			}
		}

		public function surfaceChanged(res:String):void {
			for (var i:int = 0; i < _container.numElements; i++) {
				var label:ResourceLabel = _container.getElementAt(i) as ResourceLabel;
				if (label.attachment.img == res) {
					_container.removeElementAt(i).removeEventListener(MouseEvent.CLICK, clickhandler);
					break;
				}
			}
		}

		public function hasData():Boolean {
			return _container.numElements > 0;
		}

		public function generateXml(id:String, name:String):XML {
			if (_container.numElements == 0) {
				return null;
			}
			var ROOT:XML = <map></map>;
			ROOT.@id = id;
			ROOT.@name = name;
			ROOT.@width = MapScene.getInstance().grid.width;
			ROOT.@height = MapScene.getInstance().grid.height;

			for (var i:int = 0; i < _container.numElements; i++) {
				var resource:ResourceLabel = _container.getElementAt(i) as ResourceLabel;
				var building:Building = resource.attachment;
				if (building.layer == Building.LAYER_SURFACE) {
					ROOT.@img = building.img;
				} else {
					var item:XML = <item></item>;
					item.@id = building.id;
					item.@x = building.x;
					item.@y = building.y;
					item.@img = building.img;
					item.@type = building.btype == Building.TYPE_BUILDING ? "build" : "npc";
					ROOT.appendChild(item);
				}
			}

			var grid:XML = <grid></grid>;
			grid.@cols = MapScene.getInstance().grid.cols;
			grid.@rows = MapScene.getInstance().grid.rows;
			grid.@size = MapScene.getInstance().grid.gridSize;

			for (var r:int = 0; r < grid.@rows; r++) {
				var moveable:XML = <moveable></moveable>;
				moveable.@detail = MapScene.getInstance().grid.gridStatus[r].join(',');
				grid.appendChild(moveable);
			}

			ROOT.appendChild(grid);
			return ROOT;
		}
	}
}

class SingletonEnforce {

}